import { defineStore } from "pinia";
import { ref, reactive } from "vue"
import { associateMemberPaymentPagePost } from "@/api/tcl"
import { useEnumStore } from "@/store/enumeration";
import mittBus from '@/utils/mitt';

export const paymentListPageStore = defineStore('paymentListPageStoreId1', () => {
    const { paymentStatusList, paymentTypeList, paymentMethodList } = useEnumStore()
    const refundComRef = ref<{ show: (type: string, row?: any) => void }>()

    const detailData = ref([
        {
            type: "text",
            prop: "supplierType",
            name: "供应商类型",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "orderListPage",
            propsList: [
                // {
                //     prop: 'supplierKey',
                //     label: '供应商Key',
                //     type: 'normal',
                //     minWidth: '100px',
                //     hidden: true,
                // },
                // {
                //     prop: 'supplierCode',
                //     label: '供应商编码',
                //     type: 'normal',
                //     minWidth: '100px',
                //     hidden: true,
                // },
                // {
                //     prop: 'supplierName',
                //     label: '供应商名称',
                //     type: 'normal',
                //     minWidth: '100px',
                //     hidden: true,
                // },
                {
                    prop: 'channelMerchantKey',
                    label: '渠道商Key',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                // {
                //     prop: 'channelMerchantCode',
                //     label: '渠道商编码',
                //     type: 'normal',
                //     minWidth: '150px',
                //     hidden: true,
                // },
                {
                    prop: 'channelMerchantName',
                    label: '渠道商名称',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'channelKey',
                    label: '渠道Key',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                // {
                //     prop: 'channelCode',
                //     label: '渠道编码',
                //     type: 'normal',
                //     minWidth: '290px',
                //     hidden: true,
                // },
                {
                    prop: 'mobile',
                    label: '手机号',
                    type: 'normal',
                    minWidth: '110px',
                    hidden: true,
                },
                {
                    prop: 'channelName',
                    label: '渠道名称',
                    type: 'normal',
                    minWidth: '170px',
                    hidden: true,
                },
                {
                    prop: 'productCode',
                    label: '产品编码',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'annualPayNum',
                    label: '年度缴次',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'productName',
                    label: '产品名称',
                    type: 'normal',
                    minWidth: '160px',
                    hidden: true,
                },
                {
                    prop: 'orderNo',
                    label: '订单号',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'outOrderNo',
                    label: '签约订单号',
                    type: 'normal',
                    minWidth: '210px',
                    hidden: true,
                },
                {
                    prop: 'outPaymentOrRefundOrderNo',
                    label: '支付或退款订单号',
                    type: 'normal',
                    minWidth: '210px',
                    hidden: true,
                },
                {
                    prop: 'paymentStatus',
                    label: '支付状态',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'paymentStatusDetails',
                    label: '结果',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'orderTime',
                    label: '下单时间',
                    type: 'normal',
                    minWidth: '155px',
                    hidden: true,
                },
                {
                    prop: 'signTime',
                    label: '签约时间',
                    type: 'normal',
                    minWidth: '155px',
                    hidden: true,
                },
                {
                    prop: 'nextDeductionTime',
                    label: '下次支付时间',
                    type: 'normal',
                    minWidth: '155px',
                    hidden: true,
                },

                {
                    prop: 'paymentMethod',
                    label: '支付方式',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'paymentType',
                    label: '支付类型',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },

                {
                    prop: 'paymentAmount',
                    label: '支付或退款金额',
                    type: 'custom',
                    slotName: "paymentAmount",
                    minWidth: '140px',
                    hidden: true,
                },
          
                {
                    prop: 'paymentTime',
                    label: '支付或退款时间',
                    type: 'custom',
                    slotName: "paymentTime",
                    minWidth: '155px',
                    hidden: true,
                },
          
                // {
                //     prop: 'extInfo',
                //     label: '扩展信息',
                //     type: 'normal',
                //     minWidth: '100px',
                //     hidden: true,
                // },
                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '200',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }

            ]
        })
    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            supplierCode: null, //供应商编码
            channelMerchantCodeList: null, //渠道商编码
            channelCodeList: null, //渠道编码
            orderNo: null, //订单号
            mobile: null, //手机号
            outOrderNo: null, //签约订单号
            paymentStatusList: null, //支付状态
            orderTimeStart: null, //下单时间-开始
            orderTimeEnd: null, //下单时间-结束
            outPaymentOrRefundOrderNo: null, //退款订单号
            signTimeStart: null, //签约时间-起
            signTimeEnd: null, //签约时间-止
            paymentAmountStart: null, //退款金额-起
            paymentAmountEnd: null, //退款金额-止
            paymentTimeStart: null, //退款时间-起
            paymentTimeEnd: null, //退款时间-止
            paymentTypeList: null, //支付类型 枚举
            paymentMethodList: null, //支付方式 枚举
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: [],

    })
    const operateTime = ref([])
    const getData1 = async (type: string) => {

        pageParams.loadIng = true
        if (type == 'reset') {
            operateTime.value = []
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                if (it != 'supplierCode') {
                    listSearch.condition[it] = null
                }

            })
        }
        try {
            const res = await associateMemberPaymentPagePost(listSearch)
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {
                    // paymentStatusList,paymentTypeList,paymentMethodList
                    // paymentStatus paymentType paymentMethod


                    return {
                        ...item,
                        paymentStatus: paymentStatusList[item.paymentStatus - 1]?.value,
                        paymentType: paymentTypeList[item.paymentType - 1]?.value,
                        paymentMethod: paymentMethodList[item.paymentMethod - 1]?.value,

                    }
                })
                pageParams.total = res.data.total
                pageParams.loadIng = false
            }
        } catch (error) {
            pageParams.loadIng = false
            pageParams.tableData = []
            pageParams.total = 0
        }finally{
            mittBus.emit('scrollUpdate')
        }

    }
    const handPage = (page: any) => {
        console.log(page);
        listSearch.pageNum = page.pageNum
        listSearch.pageSize = page.pageSize
        getData1('search')

    }


    const timeChange = (time: any) => {
        if (time) {
            listSearch.condition.orderTimeStart = time[0]
            listSearch.condition.orderTimeEnd = time[1]
        } else {
            listSearch.condition.orderTimeStart = null
            listSearch.condition.orderTimeEnd = null
        }
        getData1('search')
    }
    const refund = async (row: any) => {
        refundComRef.value?.show('add', row)
    }
    return {
        operateTime, detailData, tableProp, listSearch, pageParams,refundComRef,
        getData1, timeChange, handPage,refund
    }
}
/* ,{
    persist:{
        storage:localStorage,
        paths:['tableProp']
    }
} */
)

